package ro.dandanciu.webgallery.ui.galleries.sync.fs;

import java.util.Set;

import org.apache.log4j.Logger;

import ro.dandanciu.webgallery.sync.gallery.dto.GallerySyncDTO;
import ro.dandanciu.webgallery.sync.server.fs.services.FsSync;
import ro.dandanciu.webgallery.ui.general.WebGalleryAction;

public class FileSystemSynchronizeGalleryAction extends WebGalleryAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -6926644817412302244L;

	private static final String FS_VIEW = "VIEW";

	private static final String FS_SYNC = "SYNC";

	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(FileSystemSynchronizeGalleryAction.class);

	private FsSync fsSyncServices = null; 
	
	private String mode;
	
	private long galleryId;
	
	private String[] deletes = {};
	
	private String[] imports = {};
	
	private Set<GallerySyncDTO> synchronizationResult;
	
	public long getGalleryId() {
		return galleryId;
	}

	public void setGalleryId(long galleryId) {
		this.galleryId = galleryId;
	}

	
	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start");
		}

		String forward = null;
		if (FS_VIEW.equals(mode)) {
			forward = executeFileSystemSyncView();
		} else if (FS_SYNC.equals(mode)) {
			forward = executeFileSystemSync();
		} else {
			forward = setMessage("failure");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("forward: " + forward);
			logger.debug("execute() - end");
		}
		return forward;
	}

	private String executeFileSystemSync() {
		// TODO make the sync
		fsSyncServices.syncronize(galleryId, deletes, imports);
		return executeFileSystemSyncView();
	}

	private String executeFileSystemSyncView() {
		synchronizationResult = fsSyncServices.preSynchronizeDifference(galleryId);
		return "syncronize";
	}

	public String getMode() {
		return mode;
	}

	public void setMode(String mode) {
		this.mode = mode;
	}

	/**
	 * @return the synchronizationResult
	 */
	public Set<GallerySyncDTO> getSynchronizationResult() {
		return synchronizationResult;
	}

	public String[] getDeletes() {
		return deletes;
	}

	public void setDeletes(String[] deletes) {
		this.deletes = deletes;
	}

	public String[] getImports() {
		return imports;
	}

	public void setImports(String[] imports) {
		this.imports = imports;
	}

    /**
     * @param fsSyncServices the fsSyncServices to set
     */
    public final void setFsSyncServices(final FsSync fsSyncServices) {
        this.fsSyncServices = fsSyncServices;
    }
	
}
